package com.lancoo.answer.view.fragment.fill

import android.os.Bundle
import android.os.Handler
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import com.lancoo.answer.R
import com.lancoo.answer.component.InteractWebView
import com.lancoo.answer.databinding.EvViewFillBinding
import com.lancoo.answer.helper.answer.FillAnswerDialog
import com.lancoo.answer.model.AnswerConstant
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.bean.Item
import com.lancoo.answer.model.bean.Ques
import com.lancoo.answer.model.eventBean.FillAnswerEventBean
import com.lancoo.answer.model.eventBean.KeyBoardEventBean
import com.lancoo.answer.model.eventBean.SwitchQuesEventBean
import com.lancoo.answer.util.paperUtils.FragmentCommunicationUtils
import com.lancoo.answer.view.base.BaseVMFragment
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by Dadong on 2021/12/19
 * Describe: 填空题作答控件 答题点抽题 sortIndexType = 1
 */
class FillItemFragment : BaseVMFragment<EvViewFillBinding>() {

    private lateinit var ques: Ques
    private var typeIndex = 0
    private var quesIndex = 0
    private var childIndex = 0
    private var defaultLoadIndex = 0
    private var itemSortIndex: String = "" //当前选择的序号
    private var currentIndex = 0
    private var sortIndexType = "0" //抽题方式 0:小题抽题 1:答题点抽题

    override fun initView(view: View) {
        EventBus.getDefault().register(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.ev_view_fill
    }

    companion object {
        fun loadFragment(
            typeIndex: Int,
            quesIndex: Int,
            childIndex: Int,
            defaultLoadIndex: Int
        ): Fragment? {
            val mBundle = Bundle()
            mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, typeIndex)
            mBundle.putInt(AnswerConstant.KEY_QUESINDEX, quesIndex)
            mBundle.putInt(AnswerConstant.KEY_CHILDINDEX, childIndex)
            mBundle.putInt(AnswerConstant.KEY_LOADCHILDINDEX, defaultLoadIndex)
            val fillChildFragment = FillItemFragment()
            fillChildFragment.arguments = mBundle
            return fillChildFragment
        }
    }

    override fun initData() {
        if (ConstantBean.constantExamBean == null) {
            return
        }
        val kindBeanList = ConstantBean.constantExamBean!!.TypeList
        typeIndex = requireArguments().getInt(AnswerConstant.KEY_TYPEINDEX)
        quesIndex = requireArguments().getInt(AnswerConstant.KEY_QUESINDEX)
        childIndex = requireArguments().getInt(AnswerConstant.KEY_CHILDINDEX)
        var loadChildIndex = requireArguments().getInt(AnswerConstant.KEY_LOADCHILDINDEX)
        Log.e("填空题", "loadChildIndex:" + loadChildIndex + "childIndex:" + childIndex)
        ques = kindBeanList!![typeIndex].QuesList!![quesIndex]
        childIndex = ques.currentLoadChildIndex
        Log.e("填空题1", "1018:" + childIndex)
        defaultLoadIndex = ques.ChildList!![childIndex].currentLoadItemIndex
        itemSortIndex = ques.ChildList!![childIndex].ItemList!![defaultLoadIndex].ItemSortIndex!!
        sortIndexType = ques.ChildList!![childIndex].SortIndexType!!
        setInteractWebView()
    }

    //加载题干
    private fun setInteractWebView() {
        var askBody = ""
        askBody = ques.ChildList!![childIndex].ChildAsk!!
        mBinding.interactWv.isIndentation = true
        mBinding.interactWv.setDisableInterceptTouch(false)
        mBinding.interactWv.initWebView(
            requireActivity(),
            askBody!!,
            object : InteractWebView.Callback {
                override fun finishCallback() {
                    mBinding.interactWv.showOrHideAnalysisButton(0)
                    Log.e("填空题1", "itemSortIndex:" + itemSortIndex)
                    wvLoadUrl(itemSortIndex)
                }

                override fun clickCallback(position: String) {
                    itemSortIndex = position
                    wvLoadUrl(itemSortIndex)
                    wvLoadUrl(itemSortIndex)
                    showTest()
                }
            }
        )
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    //答题点下划线，高亮等等绘制
    private fun wvLoadUrl(position: String = "-1") {
        mBinding.interactWv.scrollPosition(position)
        ques.ChildList!![childIndex].ItemList!!.forEachIndexed { index, item ->
            mBinding.interactWv.loadUrl(
                item,
                position == item.ItemSortIndex.toString()
            )
            if (position == item.ItemSortIndex.toString()) {
                currentIndex = index
            }
        }
    }

    private fun showTest() {
        FillAnswerDialog.launchFillAnswerDialog(
            requireParentFragment(),
            typeIndex, quesIndex, childIndex, currentIndex
        )
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEventMainThread(bean: FillAnswerEventBean) {
        val isCurrentChildIndex = bean.childIndex == bean.lastChidIndex
        Log.e("听力题", "进来啦")
        if (typeIndex == bean.typeIndex) {
            getItemSortIndex(bean.itemIndex)
            wvLoadUrl(itemSortIndex)
            if (bean.quesIndex != quesIndex) {//切换大题
//                FragmentCommunicationUtils.switchChildIndexToViewPager(
//                    this,
//                    bean.quesIndex, bean.childIndex, bean.itemIndex
//                )
            }
        }
        Log.e("填空题2", "itemSortIndex:" + itemSortIndex)
    }

    private fun getItemSortIndex(index: Int) {
        itemSortIndex = ques.ChildList!![childIndex].ItemList!![index].ItemSortIndex!!
    }

    private fun getItemSortIndex(bean: FillAnswerEventBean) {
        itemSortIndex =
            ques.ChildList!![childIndex].ItemList!![bean.itemIndex].ItemSortIndex!!
    }

    public fun updateKeyboardShowState(isShow: Boolean) {
        Handler().postDelayed({
            mBinding.interactWv.scrollPosition(itemSortIndex)
        }, 300)
    }

    /**
     * 点击答题卡跳转对接
     */
    fun jump(index: Int) {
        if (ques == null) {
            return
        }
        getItemSortIndex(index)
        wvLoadUrl(itemSortIndex)
    }

    /**
     * 获取当前答题点下标
     */
    fun getCurrentIndex(): Int {
        return currentIndex
    }
}